﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SIE.SCADA.ZemtDemo.Machines
{
    [XmlRoot("Device")]
    public class DeviceItem
    {
        /// <summary>
        /// 设备ID
        /// </summary>
        [XmlAttribute]
        public string DeviceID { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        [XmlAttribute]
        public string DeviceType { get; set; }

        /// <summary>
        /// 设备描述
        /// </summary>
        [XmlAttribute]
        public string Desc { get; set; }


        /// <summary>
        /// 是否启用
        /// </summary>
        [XmlAttribute]
        public bool IsEnable { get; set; }


        /// <summary>
        /// 清零标志
        /// </summary>
        [XmlAttribute]
        public bool ClearFlag { get; set; }


        /// <summary>
        /// 通信协议
        /// </summary>
        [XmlAttribute]
        public string Protocol { get; set; }

        /// <summary>
        /// 监听地址
        /// </summary>
        [XmlAttribute]
        public string ListenReg { get; set; }

        /// <summary>
        /// 设备地址
        /// </summary>
        [XmlAttribute]
        public string DeviceAddress { get; set; }

        /// <summary>
        /// 清零时间
        /// </summary>
        [XmlArray("ClearTimes")]
        [XmlArrayItem("ClearTime")]
        public List<DateTime> ClearTimes { get; set; }

        /// <summary>
        /// 清零时间ID
        /// </summary>
        [XmlAttribute("ClearTimeID")]
        public int ClearTimeID { get; set; }

        /// <summary>
        /// 通信方式
        /// </summary>
        [XmlAttribute("CommWay")]
        public string CommWay { get; set; }


        /// <summary>
        /// 轮询周期
        /// </summary>
        [XmlAttribute]
        public int PollRate { get; set; }

        /// <summary>
        /// 心跳寄存器(只能是位区域)
        /// </summary>
        [XmlAttribute]
        public string HeartHeatReg { get; set; }

        /// <summary>
        /// webapi地址
        /// </summary>
        [XmlAttribute]
        public string ApiAddr { get; set; }

        /// <summary>
        /// 工序
        /// </summary>
        [XmlAttribute]
        public string ProcessNo { get; set; }
    }

    [XmlRoot("Devices")]
    public class Devices
    {
        [XmlElement("Device")]
        public List<DeviceItem> Items { get; set; }
    }

    [XmlRoot("SerialComm")]
    public class SerialModel
    {
        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public string PortName
        {
            get { return _PortName; }
            set
            {
                if (value != _PortName)
                {
                    _PortName = value;
                }
            }
        }
        private string _PortName;



        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public int BaudRate
        {
            get { return _BaudRate; }
            set
            {
                if (value != _BaudRate)
                {
                    _BaudRate = value;
                }
            }
        }
        private int _BaudRate;


        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public int DataBits
        {
            get { return _DataBits; }
            set
            {
                if (value != _DataBits)
                {
                    _DataBits = value;
                }
            }
        }
        private int _DataBits;


        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public StopBits StopBits
        {
            get { return _StopBits; }
            set
            {
                if (value != _StopBits)
                {
                    _StopBits = value;
                }
            }
        }
        private StopBits _StopBits;



        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public Parity Parity
        {
            get { return _Parity; }
            set
            {
                if (value != _Parity)
                {
                    _Parity = value;
                }
            }
        }
        private Parity _Parity;


        [XmlAttribute]
        public int TimeOut { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public string Remark
        {
            get { return _Remark; }
            set
            {
                if (value != _Remark)
                {
                    _Remark = value;
                }
            }
        }
        private string _Remark;
    }

    [XmlRoot("TcpComm")]
    public class TCPModel
    {
        [XmlAttribute]
        public string IPAddress { get; set; }


        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public int Port { get; set; }

        [XmlAttribute]
        public int TimeOut { get; set; } = 500;

        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public string Remark { get; set; }
    }

    public class ClearTimes
    {
        [XmlElement("ClearTime")]
        public List<ClearTimeItem> Items { get; set; }
    }

    [XmlRoot("ClearTime")]
    public class ClearTimeItem
    {
        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute("Time")]
        public TimeSpan ClearTime { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute]
        public string ClearReg { get; set; }
    }

}
